home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / pacsplit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  5.4 KB  |  178 lines

  1. /*
  2.   split one spectra file, containing 4 or 8 spectra, into
  3.   single spectra, possibly reverse spectra, and sum them together, to
  4.   for two spectra files, one for each phase.
  5.   Input is read from the file specified.
  6.   The peakpositions, phase and parity are read from *.def or global.def
  7.   Output goes to USP1.spc/.err and USP2.spc/.err , if not specified
  8.   with -o1 -o2.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <spec.h>
  13. #define TMPFILE "global.def"
  14.  
  15. float *spc, *err, *tim, *usp1, *usp2, *uer1, *uer2;
  16. int range=1024,
  17.     tri=0,
  18.     dev=50;
  19.  
  20. help()
  21. {
  22.   printf("Splitting spectrum file into sinngle spectra, possibly reversing\n");
  23.   printf("them, and adding them to 2 spectra (one for each phase)\n");
  24.   printf("pacsplit file [options]\n");
  25.   printf("options:\n");
  26.   printf("  -o1 file    specifies output file (0). Default: USP1\n");
  27.   printf("  -o2 file    specifies output file (90). Default: USP2\n");
  28.   printf("  -def file   specifies another definition file than %s\n",TMPFILE);
  29.   printf("The definition of the spectrum is read from %s\n",TMPFILE);
  30.   printf("This file should be in a format as generated by the FPROMPT \n");
  31.   printf("routine and is organized as follows:\n");
  32.   printf("Each line consists of 4 integers.\n");
  33.   printf("The first specifies the Time 0 position.\n");
  34.   printf("The second can be +1 -1 +2 or -2 and specifies, if it is\n");
  35.   printf("     to be mirrored (-) and if it is a 0 degree (1)\n");
  36.   printf("     or 90 degree (2) spectrum\n");
  37.   printf("     If any other number is given, the spectrum is ignored\n");
  38.   printf("The last two numbers are used to specify the range\n");
  39.   printf("of the usable data area. Up to now, these parameters\n");
  40.   printf("are only significant in the last line !\n");
  41.   printf("\n(C) Rainer Kowallik\n");
  42.   exit(0);
  43. }
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49. int  xbeg,xend,n,m,i,max,peakptr,pacmax,peaks[30],phase[30];
  50. char z[80],comment[80],nam1[80],nam2[80],defnam[80];
  51. char *home;
  52. float x;
  53. FILE *fp;
  54.  
  55.    strcpy(nam1,"USP1");
  56.    strcpy(nam2,"USP2");
  57.    strcpy(defnam,TMPFILE);
  58.  
  59.    if(checkopt(argc,argv,"-o1",z)) strcpy(nam1,z);
  60.    if(checkopt(argc,argv,"-o2",z)) strcpy(nam2,z);
  61.    if(checkopt(argc,argv,"-def",z)) strcpy(defnam,z);
  62.  
  63.    spc=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  64.    err=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  65.    tim=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  66.    usp1=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  67.    usp2=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  68.    uer1=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  69.    uer2=(float *)calloc(_MAXSPCLEN+2,sizeof(float));
  70.  
  71. /* -----------------------------------------------
  72.    reading spectrum 
  73.    ----------------------------------------------- */
  74.  
  75.    max=readspec(argv[1],spc,err,tim,comment);
  76.  
  77. /* -----------------------------------------------
  78.    reading spectra definition file
  79.    ----------------------------------------------- */
  80.  
  81.    fp=fopen(defnam,"r");
  82. #ifdef UNIX
  83.    if(fp==NULL) {
  84.       home = (char *) getenv("HOME");
  85.       if(home != NULL) {strcpy(defnam,home); strcat(defnam,"/"); }
  86.       strcat(defnam,TMPFILE);
  87.       fp=fopen(defnam,"r");
  88.    }
  89.    if(fp==NULL) {
  90.       home = (char *) getenv("LISEPRG");
  91.       if(home != NULL) {strcpy(defnam,home); strcat(defnam,"/"); }
  92.       strcat(defnam,TMPFILE);
  93.       fp=fopen(defnam,"r");
  94.    }
  95. #endif
  96. #ifdef AMIGA
  97.    if(fp==NULL) {
  98.       strcpy(defnam,"LISE:"); strcat(defnam,TMPFILE);
  99.       fp=fopen(defnam,"r");
  100.    }
  101. #endif
  102.    if(fp == NULL) {
  103.       printf("rvt2: could not open >%s<\n",TMPFILE);
  104.       exit(0);
  105.    }
  106.    peakptr=0;
  107.    while(!feof(fp)) {
  108.       fscanf(fp,"%d %d %d %d\n",&peaks[peakptr],&phase[peakptr],&i,&pacmax);
  109.       peakptr=peakptr+1;
  110.    }
  111.    fclose(fp);
  112.  
  113. /* -------------------------------------------------
  114.    setting usp1, usp2 ,uer1 and uer2 to 0
  115.    ------------------------------------------------- */
  116.  
  117.    for(n=0;n<pacmax;n++) {
  118.       usp1[n]=0.0;
  119.       usp2[n]=0.0;
  120.       uer1[n]=0.0;
  121.       uer2[n]=0.0;
  122.    }
  123.  
  124. /* ----------------------------------------------
  125.    here we start calculating the new sum spectra 
  126.   -----------------------------------------------*/
  127.  
  128.    for(n=0;n<peakptr;n++) {
  129.       xbeg = peaks[n];
  130.       m = phase[n];
  131.       switch(m) {     /* which spectrum to add ? reversing ? */
  132.       case -2:        /* mirror, 90 degree spectrum */
  133.      for(i=0;i<pacmax;i++) {
  134.         usp2[i] = usp2[i] + spc[xbeg-i];
  135.         uer2[i] = uer2[i] + err[xbeg-i];
  136.          }
  137.      break;
  138.       case -1:       /* mirror 0 degree spectrum */
  139.      for(i=0;i<pacmax;i++) {
  140.         usp1[i] = usp1[i] + spc[xbeg-i];
  141.         uer1[i] = uer1[i] + err[xbeg-i];
  142.          }
  143.      break;
  144.       case 1:       /* normal 0 degree spectrum */
  145.      for(i=0;i<pacmax;i++) {
  146.         usp1[i] = usp1[i] + spc[xbeg+i];
  147.         uer1[i] = uer1[i] + err[xbeg+i];
  148.          }
  149.      break;
  150.       case 2:       /* normal 90 degree spectrum */
  151.      for(i=0;i<pacmax;i++) {
  152.         usp2[i] = usp2[i] + spc[xbeg+i];
  153.         uer2[i] = uer2[i] + err[xbeg+i];
  154.          }
  155.      break;
  156.       }
  157.    }
  158.    for(i=0;i<pacmax;i++) { /* generate new error spectrum */
  159.       uer1[i] = (float) sqrt((double) usp1[i]);
  160.       uer2[i] = (float) sqrt((double) usp2[i]);
  161.    }
  162.  
  163. /* -------------------------------------------
  164.    now we can go and save the new spectra 
  165.    ------------------------------------------- */
  166.  
  167.    strcpy(z,comment);
  168.    strcat(z," | 0");
  169.    writespec(nam1,usp1,uer1,pacmax,1,z);
  170.    strcpy(z,comment);
  171.    strcat(z," | 90");
  172.    writespec(nam2,usp2,uer2,pacmax,1,z);
  173.    free(uer1); free(uer2); free(usp1); free(usp2);
  174.    free(spc); free(err); free(tim);
  175.    exit(0);
  176. }
  177.  
  178.